package edu.indiana.d2i.ontology.exec;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;

import edu.indiana.d2i.ontology.AbstractSQLExec;
import edu.indiana.d2i.ontology.util.AccessDBUtil;
import edu.indiana.d2i.ontology.util.Constants;

public class Plot extends AbstractSQLExec {
	private final String PREFIX = "plot_";
	private final String SQLPREFIX = "select * from PLOT where ID=";
	
	public Plot(AccessDBUtil dbCon, OntModel model, int id, String subjectURI) {
		super(dbCon, model, id, subjectURI);
		resourceURI = Constants.NAME_SPACE + PREFIX + id; 		// set the URI for the resource
		sqlStatement = SQLPREFIX + id;				// set SQL to get one row from DB
		individual = 								// create individual (instance)
			model.getOntClass(Constants.FOREST_PLOT_CLASS).createIndividual(resourceURI);
	}

	@Override
	protected void addObjectProperties(String subjectURI) {
		ObjectProperty pHasChildRelation = model.createObjectProperty(Constants.HAS_COMPONENT);
		
		try {
			// add parent object properties
			if (subjectURI != null) {
				ObjectProperty pHasParentRelation = model.getObjectProperty(Constants.HAS_RELATIONSHIP);
				model.add(individual, pHasParentRelation, model.getIndividual(subjectURI));
			}
			
			// add child object properties
			List<AbstractSQLExec> childList = new ArrayList<AbstractSQLExec>();
			
			// add "Has_component some Ground_cover_and_seedlings_sample"
			sqlStatement = "SELECT pg.ID from P_GCOVER AS pg where pg.FK_PLOT=" + id;
			ResultSet resultSet = dbCon.query(sqlStatement);
			while (resultSet.next()) {
				int gcoverID = resultSet.getInt("id");
				GCover gcover = new GCover(dbCon, model, gcoverID, resourceURI);
				childList.add(gcover);
			}
			
			// add "Has_component some Shrubs_and_sapplings_sample" and 
			// "Has_component some Tree_sample"
			sqlStatement = "SELECT * from P_INFO AS pi where pi.FK_PLOT=" + id;
			resultSet = dbCon.query(sqlStatement);
			while (resultSet.next()) {
				int compID = resultSet.getInt("id");
				String ptype = resultSet.getString("P_TYPE");
				if (ptype.equalsIgnoreCase("B") || ptype.equalsIgnoreCase("P") ||
					ptype.equalsIgnoreCase("L") || ptype.equalsIgnoreCase("W")) {
					// shrub stuffs
					ShrubSapplingSample shrubsample = 
						new ShrubSapplingSample(dbCon, model, compID, resourceURI);
					childList.add(shrubsample);
				}
				else {
					// tree stuffs
					TreeSample treesample = new TreeSample(dbCon, model, compID, resourceURI);
					childList.add(treesample);
				}
			}
			
			for (AbstractSQLExec child : childList) {
				child.setData2JenaModel();
				model.add(individual, pHasChildRelation, child.getIndividual());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
